import pygame
import time
import random

pygame.init()

yellow = (255, 255, 0)
black = (0, 0, 0)
green = (0, 255, 0)
blue = (50, 153, 213)

grid = 16
canvas_width = grid * 25
canvas_height = grid * 25

snake_speed = 15

canvas = pygame.display.set_mode((canvas_width, canvas_height))
pygame.display.set_caption('Snake Game')

clock = pygame.time.Clock()

font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 25)


def game_loop():

    x = grid * 6
    y = grid * 6

    xd = grid
    yd = 0
    
    score = 0

    snake_list = []
    length_of_snake = 5

    foodx = round(random.randrange(0, canvas_width - grid) / grid) * grid
    foody = round(random.randrange(0, canvas_height - grid) / grid) * grid

    while True:

        clock.tick(snake_speed)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT and xd != grid:
                    xd = -grid
                    yd = 0
                elif event.key == pygame.K_RIGHT and xd != -grid:
                    xd = grid
                    yd = 0
                elif event.key == pygame.K_UP and yd != grid:
                    yd = -grid
                    xd = 0
                elif event.key == pygame.K_DOWN and yd != -grid:
                    yd = grid
                    xd = 0

        # 移动蛇
        x += xd
        y += yd
        snake_head = (x, y)
        snake_list.insert(0,snake_head)
        if len(snake_list) > length_of_snake:
            snake_list.pop()
            
        # 画图
        pygame.display.update()
        canvas.fill(black)
        pygame.draw.rect(canvas, green, [foodx, foody, grid-1, grid-1])
        value = score_font.render(
            "Your Score: " + str(score), True, yellow)
        canvas.blit(value, [0, 0])

        for i in snake_list:
            pygame.draw.rect(canvas, blue, [i[0], i[1], grid-1, grid-1])

        # 判断是否出界
        if x >= canvas_width or x < 0 or y >= canvas_height or y < 0:
            return

        # 判断头结点和其他节点是否重合
        #i = 1
        #while i< len(snake_list):
        #    if snake_head == snake_list[i]:
        #        return 
        #    i = i+1
        
        for i in snake_list[1:]:
            if i == snake_head:
                return

        # 吃到了食物
        if x == foodx and y == foody:
            foodx = round(random.randrange(
                0, canvas_width - grid) / grid) * grid
            foody = round(random.randrange(
                0, canvas_height - grid) / grid) * grid
            length_of_snake += 5
            score += 1

while True:

    canvas.fill(black)
    mesg = font_style.render("Press C for Play or Q for Quit", True, yellow)
    canvas.blit(mesg, [canvas_width / 6, canvas_height / 3])
    pygame.display.update()
    time.sleep(1)

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
            if event.key == pygame.K_c:
                game_loop()
